﻿using Microsoft.EntityFrameworkCore;

namespace EntityModels.DbContexts
{
    /// <summary>
    /// 自定义数据库函数和映射
    /// </summary>
    public class DbFunctionDbContext : DbContext
    {
        [DbFunction(Name = "MyFunction", Schema = "dbo")]
        public static int MyFunction(int value)
        {
            throw new NotImplementedException();
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            //风格一
            modelBuilder.HasDbFunction(() => MyFunction(default))
                .HasName("MyFunction")
                .HasSchema("dbo");

            //风格二
            modelBuilder.HasDbFunction(GetType().GetMethod(nameof(DbFunctionDbContext.MyFunction)),
                b =>
                {
                    b.HasName("MyFunction")
                        .HasSchema("dbo");
                });
        }

    }
}
